from LeveragedOrderPlacement import execute_leveraged_trade
from PositionObservation import check_and_close_position
from ConfigurationData import symbol,client,leverage,profit_threshold
from ConfigurationData import ConfigurationData


import random


def choose_trade_direction():
    """执行交易操作"""
    # 如果没有挂单，则执行随机买卖操作
    position_info = client.futures_position_information(symbol=symbol)
    position = next((p for p in position_info if p['symbol'] == symbol), None)
    flag = position['positionAmt'] == '0.00'
    if  flag:
        # 随机选择买或卖
        side = 'BUY' if random.choice([True, False]) else 'SELL'
        # 补充买卖操作逻辑
        # 执行交易
        execute_leveraged_trade(client, symbol, leverage ,side)
        ConfigurationData.orderingCount += 1
        print("下单成功了"+str(ConfigurationData.orderingCount)+"次"+"，其中成功了"+str(ConfigurationData.successCount)+"次")
    # 如果有挂单，则检查盈利情况
    else:
        check_and_close_position(client, symbol, profit_threshold)